package servlet.community;

import org.junit.Test;
import service.community.CommunityService;
import service.community.CommunityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DelCommunityServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String communityId =  req.getParameter("Community_id");
        CommunityService communityService = new CommunityServiceImpl();

        System.out.println("要删除的社区id："+communityId);
        if(communityId == null){
            System.out.println("删除，异常操作！");
            req.getRequestDispatcher("/admin/error/error404.jsp").forward(req,resp);
        } else if (communityService.getCommunityInfo(Integer.valueOf(communityId)) == true){
            System.out.println("room表中有这个id为"+Integer.valueOf(communityId)+"的数据!");
            req.setAttribute("message", "删除失败，社区有关联");
//            req.getRequestDispatcher("/admin/error/error404.jsp").forward(req,resp);
        } else {
            communityService.deleteCommunity(Integer.valueOf(communityId));
            System.out.println("删除成功！");
            req.setAttribute("message","社区删除成功！");
        }
        req.getRequestDispatcher("CommunityInfo").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Test
    public void test(){
        int community_id = 1;
        CommunityServiceImpl communityService = new CommunityServiceImpl();
        System.out.println("删除语句是否成功："+communityService.deleteCommunity(community_id));

    }
}
